<template>
  <el-dialog :title="info.title" append-to-body :visible.sync="visible" center width="460px" :before-close="handleCancel">
    <el-form ref="formDate" label-suffix=":" label-width="100px" :rules="rules" :model="form">
      <el-row>
        <el-col :span="24">
          <el-form-item label="用户名称" prop="user">
            <el-select
              v-model="form.user"
              value-key="key"
              size="small"
              :remote-method="selUser"
              remote
              filterable
              style="width: 100%"
              placeholder="请选择员工"
              clearable
              @focus="selUser"
            >
              <el-option
                v-for="item in users"
                :key="item.key"
                :label="item.value"
                :value="item"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <div class="fr mt10">
            <el-button size="small" @click="handleCancel()">取 消</el-button>
            <el-button type="primary" size="small" @click="handleEdit()">确 定</el-button>
          </div>
        </el-col>
      </el-row>
    </el-form>
  </el-dialog>
</template>

<script>
import { selUser } from '@/api/organ'

export default {
  props: {
    info: {
      type: Object,
      default: null
    },
    getData: {
      type: Function,
      default: function() {}
    }
  },
  data() {
    return {
      form: {},
      users: [],
      rules: {
        user: [{ required: true, trigger: 'blur', message: '请选择用户' }]
      },
      visible: false
    }
  },
  watch: {
    visible(val) {
      if (val) {
        this.$set(this, 'form', this.info.edit)
      }
    },
    getData: {
      type: Function,
      default: function() {}
    }
  },
  mounted() {
  },
  methods: {
    selUser(query) {
      let str = ''
      if (Object.prototype.toString.call(query) === '[object String]') str = query
      selUser({ name: str }).then(response => {
        this.users = response.list
      }).catch(err => {
        this.$message.error(err.msg)
      })
    },
    handleEdit() {
      this.$refs['formDate'].validate(async(valid) => {
        if (valid) {
          this.getData(this.form.user)
          this.handleCancel()
        }
      })
    },
    handleCancel(done) {
      if (this.$refs['formDate']) this.$refs['formDate'].resetFields()
      if (done && typeof done === Function) {
        done()
      } else {
        this.visible = false
      }
    }
  }
}
</script>

<style>

</style>
